package com.bocai.utils;


import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 对象相关的基础工具类
 * @author zhengchb
 */
public class ObjUtils {
	private static Random random = new SecureRandom();
	/**
	 * obj is empty Object
	 * @param obj
	 * @return
	 */
	public static boolean isEmpty(Object obj){
		return (obj == null) || "".equals(obj);
	}
	
	/**
	 * obj is not empty Object
	 * @param obj
	 * @return
	 */
	public static boolean isNotEmpty(Object obj){
		return !isEmpty(obj);
	}

	/**
	 * obj is an array class
	 * @param obj
	 * @return
	 */
	public static boolean isArray(Object obj) {
		return (obj != null) && (obj.getClass().isArray());
	}

	/**
	 * Object[] is empty
	 * @param array
	 * @return
	 */
	public static boolean isEmpty(Object[] array) {
		return (array == null) || (array.length == 0);
	}
	
	/**
	 * Object[] is empty
	 * @param array
	 * @return
	 */
	public static boolean isEmpty(String[] array) {
		return (array == null) || (array.length == 0);
	}
	
	/**
	 * Object[] is not empty
	 * @param array
	 * @return
	 */
	public static boolean isNotEmpty(Object[] array) {
		return !isEmpty(array);
	}
	
	/**
	 * Object[] is not empty
	 * @param array
	 * @return
	 */
	public static boolean isNotEmpty(String[] array) {
		return !isEmpty(array);
	}

	/**
	 * 判断某对象是否为Boolean且值为真
	 * @param value
	 * @return
	 */
	public static boolean getBoolean(Object value) {
		if (value instanceof Boolean) {
			return ((Boolean) value).booleanValue();
		} else {
			return false;
		}
	}
	
	/**
	 * obj is empty Object return null
	 * @param <T>
	 * @param obj
	 * @return
	 */
	public static <T> T isEmptyReturnNull(T obj){
		if(isEmpty(obj)){
			return null;
		}
		return obj;
	}
	
	/**
	 * obj is empty Object return null
	 * @param obj
	 * @return
	 */
	public static String[] isEmptyReturnNull(String[] obj){
		if(isEmpty(obj)){
			return null;
		}
		return obj;
	}
	public static String randomNumeric(Integer len) {
		return random
				.ints(len, 0, 10)
				.mapToObj(String::valueOf)
				.reduce((s, s1) -> s + s1).get();
	}
	public static String getNowDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	public static String getOrderNo(){
        return "GYT"+ObjUtils.getNowDate() + "00100"+ObjUtils.randomNumeric(4);
    }

    public static String getUserNo(String userCount){

	    String userNo = "GYT100001";

        userNo = userNo.substring( 0 , userNo.length()-userCount.length() ) + userCount;

        System.out.println("userNo = " + userNo);

        return userNo;
    }

    /**
     * 校验参数
     * @param obj
     * @return
     */
    public static CodeEnum validateParams(Object [] obj){
        for (Object object : obj){
			if(object == null || object.equals("")){
				//设置返回数据
				return CodeEnum.CODE_90001;
			}
		}
        return null;
    }
    public static void main(String args[]){
        getUserNo("12");
    }
}
